//
//  PZRClickDetailViewController.m
//  Moment
//
//  Created by pang on 16/1/5.
//  Copyright © 2016年 片刻. All rights reserved.
//

#import "PZRClickDetailViewController.h"

@interface PZRClickDetailViewController ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *webView;
@end

@implementation PZRClickDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleStr;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake( 0 , 64 , SCREEN_WIDTH, SCREEN_HEIGHT - 64 )];
    _webView.backgroundColor = [UIColor clearColor];
    [(UIScrollView *)[[_webView subviews] objectAtIndex:0] setBounces:NO];
    NSString *urlStr = @"http://api2.pianke.me/article/info";
    NSString *bodyStr = [NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&version=3.0.6", self.contentId];
    //NSLog(@"!!##!!%@",self.contentId);
    [GetData postUrl:urlStr body:bodyStr result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        [_webView loadHTMLString:[dic objectForKey:@"html"] baseURL:nil];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        [self.view addSubview:_webView];
    } failure:^(NSError *error) {
        
        
    }];

}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isNight"] isEqualToString:@"YES"]) {
        //        self.homeDataTableView.backgroundColor = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1];
        self.view.backgroundColor = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1];
        //字体大小
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '330%'"];
        //字体颜色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
        //页面背景色
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
    }
    
    
    NSString *str1 = [NSString stringWithFormat:@"%0.2f",SCREEN_WIDTH-20];
    //webview 自适应高度
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:
                                                     @"var script = document.createElement('script');"
                                                     "script.type = 'text/javascript';"
                                                     "script.text = \"function ResizeImages() { "
                                                     "var myimg,oldwidth;"
                                                     "var maxwidth= %@;"//缩放系数,调动这个参数可以改变图片的宽度
                                                     "for(i=0;i <document.images.length;i++){"
                                                     "myimg = document.images[i];"
                                                     "oldwidth = myimg.width;"
                                                     "if (oldwidth >= 300) {"
                                                     "myimg.width = maxwidth;"
                                                     "myimg.height = myimg.height;"
                                                     "}"
                                                     "}"
                                                     "}\";"
                                                     "document.getElementsByTagName('head')[0].appendChild(script);", str1]];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
    [webView stringByEvaluatingJavaScriptFromString:str];
    
    [self hideWaiting];
    
}
-(void)hideWaiting
{
    [[self.view viewWithTag:9998] removeFromSuperview];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
